package com.bw.services.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bw.services.R
import com.bw.services.entity.NewsEntity

class NewsAdapter(private val newsList: List<NewsEntity>) : RecyclerView.Adapter<NewsAdapter.ViewHolder>() {

    var onItemClickListener: ((Int, NewsEntity) -> Unit)? = null
    var onViewDetailsClickListener: ((Int, NewsEntity) -> Unit)? = null

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val tvNewsTitle: TextView = view.findViewById(R.id.tv_news_title)
        val tvNewsContent: TextView = view.findViewById(R.id.tv_news_content)
        val tvNewsDate: TextView = view.findViewById(R.id.tv_news_date)
        val btnViewDetails: Button = view.findViewById(R.id.btn_view_details)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_news, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val news = newsList[position]
        
        holder.tvNewsTitle.text = news.title
        holder.tvNewsContent.text = news.content
        holder.tvNewsDate.text = news.publishDate
        
        // 设置点击事件
        holder.itemView.setOnClickListener {
            onItemClickListener?.invoke(position, news)
        }
        
        holder.btnViewDetails.setOnClickListener {
            onViewDetailsClickListener?.invoke(position, news)
        }
    }

    override fun getItemCount() = newsList.size

} 